package com.heima.comment.service.impl;

import com.heima.comment.dto.CommentStreamMessage;
import com.heima.comment.entity.ApComment;
import com.heima.comment.entity.ApCommentRepay;
import com.heima.comment.service.ICommentService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

/**
 * @Author WSJ
 * @Date 2022-03-29-21:11
 * @Version 1.0 （版本号）
 */
@Service
public class CommentServiceImpl implements ICommentService {
    @Autowired
    private MongoTemplate template;
    @Override
    public void update(CommentStreamMessage commentStreamMessage) {
        //修改数据库中评论的喜欢数和评论数
        ApComment apComment = template.findById(new ObjectId(commentStreamMessage.getCommentId()), ApComment.class);
        if (apComment!=null) {
            apComment.setLikes(apComment.getLikes() + commentStreamMessage.getLike());
            apComment.setReply(apComment.getReply().intValue() + commentStreamMessage.getComment().intValue());
            template.save(apComment);
        }else {
            ApCommentRepay commentRepay = template.findById(new ObjectId(commentStreamMessage.getCommentId()), ApCommentRepay.class);
            commentRepay.setLikes(commentRepay.getLikes() + commentStreamMessage.getLike());
            template.save(commentRepay);
        }
    }
}
